def snuaVid(x):
	stok = []
	skil = ""
	teljari = 0
	for y in range(len(x)):
		if x[y] == "[":
			stok.append(x[y+1])
			if len(stok) > 1:
				teljari += 1
		if x[y] == "[" and x[y+2] != "]":
			stok[teljari] += x[y+2]
			if y+3 < len(x) and x[y+3] != "]":
				stok[teljari] += x[y+3]
				if y+4 < len(x) and x[y+4] != "]":
					stok[teljari] += x[y+4]
		
	for x in range(len(stok)-1, -1, -1):
		if x < len(stok)-1:
			skil += "[" + stok[x] + "]->"
		else:
			skil += "[" + stok[x] + "]->"
	skil += "NULL"
	return skil
	
oft = int(input())

listar = []

for x in range(oft):
	t = input()
	listar.append(snuaVid(t))
for x in listar:
	print(x)
	
	
